package com.xmqian.app.project.ui.user.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.coszero.common.base.mvp.BaseMvpActivity;
import com.coszero.ui.dialog.ToastUtils;
import com.coszero.ui.widget.CountDownTextView;
import com.xmqian.app.project.event.LoginEvent;
import com.xmqian.app.project.ui.main.MainActivity;
import com.xmqian.app.project.ui.user.contract.LoginContract;
import com.xmqian.app.project.ui.user.model.LoginModel;
import com.xmqian.app.project.ui.user.presenter.LoginPresenter;
import com.xmqian.kotlinApp.R;
import com.xmqian.kotlinApp.databinding.ActivityLoginQuickBinding;

import org.greenrobot.eventbus.EventBus;


public class QuickLoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View {


    CountDownTextView mTvGetCode;
    Button mBtnLogin;
    EditText mEtUser;
    EditText mEtPwd;
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setLoginBtnState();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    private ActivityLoginQuickBinding binding;


    @Override
    protected LinearLayout getLayoutView() {
        setShowBack(false);
        setTitle("快捷登录");
        binding = ActivityLoginQuickBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected LoginPresenter getPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void initView() {
        mEtPwd = binding.etSmsCode;
        mEtUser = binding.etUser;
        mEtPwd.addTextChangedListener(textWatcher);
        mEtUser.addTextChangedListener(textWatcher);
    }

    @Override
    protected void initData() {

    }

    /**
     * 注册
     *
     * @param view
     */
    public void register(View view) {
        startActivity(new Intent(this, com.xmqian.app.project.ui.user.activity.RegisterActivity.class));
    }

    private void getSmsCode() {
        if (mEtUser.length() == 11) {
            mPresenter.getSmsCode2(3, mEtUser.getText().toString());
        } else {
            ToastUtils.showMsg("手机号长度不正确");
        }
    }

    void onClick(View v) {
        if (v.getId() == R.id.btn_login) { // 登录
            String userName = mEtUser.getText().toString();
            String passWord = mEtPwd.getText().toString();
            mPresenter.quickLogin(userName, passWord);
        } else if (v.getId() == R.id.tv_get_code) { // 获取验证码
            getSmsCode();
        } else if (v.getId() == R.id.tv_pass_login) { // 跳转密码登录
            com.xmqian.app.project.ui.user.activity.LoginActivity.startTask(this);
        }
    }

    @Override
    public void onNetworkError() {
        hideLoadingDialog();
    }

    public void setLoginBtnState() {
        if (checkUserInput()) {
            mBtnLogin.setEnabled(true);
        } else {
            mBtnLogin.setEnabled(false);
        }
    }

    public boolean checkUserInput() {
        return mEtUser.length() == 11 && mEtPwd.length() >= 4;
    }

    @Override
    public void onLoginSucceed(LoginModel data) {
        ToastUtils.showMsg("登录成功");
        startActivity(new Intent(this, MainActivity.class));
        EventBus.getDefault().post(new LoginEvent(true));
    }

    @Override
    public void onRequestFailed(String message) {

    }

    @Override
    public void getCodeSuccess() {
        //验证码发送成功
    }
}
